package com.example.ll_video.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.ll_video.MyApplication;
import com.example.ll_video.R;
import com.example.ll_video.activity.AboutActivity;
import com.example.ll_video.activity.LoginActivity;
import com.example.ll_video.activity.OpinionActivity;
import com.example.ll_video.activity.PayActivity;
import com.example.ll_video.activity.SettingActivity;
import com.example.ll_video.activity.ShareActivity;
import com.example.ll_video.bean.UserInfo;
import com.example.ll_video.utils.CommonDialog;
import com.example.ll_video.utils.JsonUtil;
import com.example.ll_video.utils.SPUtils;
import com.example.ll_video.utils.ToastUtils;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIPackageHelper;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView2;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RightFragment extends Fragment {

    @BindView(R.id.iv_user_avatar)
    QMUIRadiusImageView2 ivUserAvatar;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.iv_icon_time)
    ImageView ivIconTime;
    @BindView(R.id.tv_vip_time)
    TextView tvVipTime;
    @BindView(R.id.iv_icon_qq)
    ImageView ivIconQq;
    @BindView(R.id.tv_kefu_qq_title)
    TextView tvKefuQqTitle;
    @BindView(R.id.tv_kefu_qq_account)
    TextView tvKefuQqAccount;
    @BindView(R.id.btn_copy_qq_account)
    QMUIRoundButton btnCopyQqAccount;
    @BindView(R.id.iv_icon_wechat)
    ImageView ivIconWechat;
    @BindView(R.id.tv_kefu_wechat_title)
    TextView tvKefuWechatTitle;
    @BindView(R.id.tv_kefu_wechat_account)
    TextView tvKefuWechatAccount;
    @BindView(R.id.btn_copy_wechat_account)
    QMUIRoundButton btnCopyWechatAccount;
    @BindView(R.id.iv_icon_yijian)
    ImageView ivIconYijian;
    @BindView(R.id.iv_icon_about)
    ImageView ivIconAbout;
    @BindView(R.id.iv_icon_clean)
    ImageView ivIconClean;
    @BindView(R.id.tv_cache)
    TextView tvCache;
    @BindView(R.id.iv_icon_version)
    ImageView ivIconVersion;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.tv_chongzhi)
    TextView tvChongzhi;
    @BindView(R.id.tv_tuiguang)
    TextView tvTuiguang;
    @BindView(R.id.rl_chongzhi)
    RelativeLayout rlChongzhi;
    @BindView(R.id.ll_yijian)
    RelativeLayout llYijian;
    @BindView(R.id.rl_about)
    RelativeLayout rlAbout;
    @BindView(R.id.rl_clean)
    RelativeLayout rlClean;
    private View view;
    private Handler handler = new Handler(Objects.requireNonNull(Looper.myLooper())) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            ToastUtils.showTextToast(getContext(), "缓存已清除");
            tvCache.setText("0.0KB");
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.right_fragment_math, container, false);
        ButterKnife.bind(this, view);
        setUserInfo();
        if (MyApplication.getInstance().getOtherInfo() != null)
            tvKefuWechatAccount.setText(MyApplication.getInstance().getOtherInfo().getWeixin());
        else tvKefuWechatAccount.setText("");
        try {
            tvCache.setText(SPUtils.getTotalCacheSize(Objects.requireNonNull(getContext())));
        } catch (Exception e) {
            e.printStackTrace();
        }
        tvVersion.setText(String.format("V_%s", QMUIPackageHelper.getAppVersion(getContext())));
        return view;
    }

    private void setUserInfo() {
        UserInfo userInfo1 = null;
        final String sUserInfo = (String) SPUtils.get(getContext(), "userInfo", "");
        if (!TextUtils.isEmpty(sUserInfo)) {
            JSONObject jsonObject = JSON.parseObject(sUserInfo);
            userInfo1 = JsonUtil.jsonToObject(jsonObject.getString("userinfo"), UserInfo.class);
            MyApplication.getInstance().setUserInfo(userInfo1);
        }

//        ivUserAvatar.setImageBitmap(Base64Bitmap.stringToBitmap(userInfo.getAvatar()));
//        ivUserAvatar2.setImageBitmap(Base64Bitmap.stringtoBitmap(userInfo.getAvatar()));
        if (userInfo1 != null && !TextUtils.isEmpty(userInfo1.getUsername())) {
            tvUserName.setText(userInfo1.getUsername());
        } else {
            tvUserName.setText("未登录");
            tvUserName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
            });
        }

    }

    CommonDialog commonDialog;

    @OnClick({R.id.tv_chongzhi, R.id.tv_tuiguang, R.id.rl_chongzhi, R.id.btn_copy_qq_account, R.id.btn_copy_wechat_account,
            R.id.ll_yijian, R.id.rl_about, R.id.rl_clean, R.id.rl_exit})
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.ll_yijian:
                intent.setClass(getActivity(), OpinionActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_chongzhi:
            case R.id.rl_chongzhi:
                intent.setClass(getActivity(), PayActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_tuiguang:
                intent.setClass(getActivity(), ShareActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_about:
                intent.setClass(getActivity(), AboutActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_copy_qq_account:
                setClipboard(tvKefuQqAccount.getText().toString());
                break;
            case R.id.btn_copy_wechat_account:
                setClipboard(tvKefuWechatAccount.getText().toString());
                break;
            case R.id.rl_clean:
                commonDialog = new CommonDialog(getContext(), "确认是否清除缓存", "取消", "确认", new CommonDialog.DialogClick() {
                    @Override
                    public void selectBtn(String str, int i) {
                        commonDialog.closeDialog();
                        if (i == 1) {
                            handler.sendEmptyMessageDelayed(1, 500);
                        }
                    }
                });
                commonDialog.setTitle("清除缓存");
                commonDialog.showDialog();
                break;
            case R.id.rl_exit:
                SPUtils.clear(getContext());
                intent = new Intent(getContext(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
                break;
        }
    }

    /**
     * 打电话
     *
     * @param phoneNum
     */
    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }
    /**
     * 复制
     * @param text
     */
    public void setClipboard(String text) {
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", text);
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
        ToastUtils.showTextToast(getActivity(),"复制成功");
    }
}
